package cn.lyf.dao;

import cn.lyf.entity.Answer;
import cn.lyf.entity.Blog;
import cn.lyf.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CommentDaoImpl implements CommentDao {
    @Autowired
    MongoTemplate mongoTemplate;

    //添加回复
    @Override
    public Comment addComment(Comment comment) {
        return  mongoTemplate.save(comment);
    }

    //根据博客文章id查找对应的博客的评论集合
    @Override
    public List<Comment> findCommentsByBlogid(String blogid){
        Query query = new Query(Criteria.where("id").is(blogid));
        Blog blog = mongoTemplate.findOne(query,Blog.class);
        return blog.getComments();
    }

    //根据id查找评论
    @Override
    public Comment findById(String id) {
        System.out.println("id=="+id);
        Query query = new Query(Criteria.where("id").is(id));
        return mongoTemplate.findOne(query,Comment.class);
    }

    @Override
    public void delOneComment(Comment comment) {
         mongoTemplate.remove(comment);
    }
}
